package com.example.lotterysystem.service.mq;

import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.common.utils.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.example.lotterysystem.common.config.DirectRabbitConfig.*;

@Slf4j
@Component
@RabbitListener(queues = DLX_QUEUE_NAME)
// 死信队列
public class MqDlxReceiver {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailUtil mailUtil;
    @RabbitHandler
    public void process(Map<String, String> message){
        log.info("处理死信队列的异常消息");
        mailUtil.sendSampleMail("renht72@163.com",
                "消息消费失败", JacksonUtil.writeValueAsString(message));
        rabbitTemplate.convertAndSend(QUEUE_NAME, ROUTING, message);
    }
}
